package com.qingzhuge.mq.consumer.autoconfigure;

import com.qingzhuge.common.concurrent.Executors;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.listener.DefaultMessageListenerContainer;

import javax.jms.ConnectionFactory;


/**
 * @author : zero.xiao
 * @description :
 * @date :2019-06-19 11:04
 * @modified :
 */
@ComponentScan("org.springframework.boot.autoconfigure.jms")
@Configuration
@EnableJms
@ConditionalOnBean(ConnectionFactory.class)
public class AutoMqConsumerConfiguration {
    @Bean
    public JmsListenerContainerFactory<DefaultMessageListenerContainer> topicListenerFactory(ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setPubSubDomain(true);
        factory.setConnectionFactory(connectionFactory);
        //并发消费
        factory.setTaskExecutor(Executors.newFixedThreadPool("JmsListenerTopic",10));
        factory.setConcurrency("10");
        return factory;

    }
    @Bean
    public JmsListenerContainerFactory<DefaultMessageListenerContainer> queueListenerFactory(ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setPubSubDomain(false);
        factory.setConnectionFactory(connectionFactory);
        //并发消费
        factory.setTaskExecutor(Executors.newFixedThreadPool("JmsListenerQueue",10));
        factory.setConcurrency("10");
        return factory;
    }
}
